const template = require(process.cwd() + '/src/template/template.js')

class DataReplace {
  static templateReplace (tableData) {
    let columnsStr = tableData.columns.map(column => {
      return column.COLUMN_KEY 
      ? `  // ${column.COLUMN_COMMENT}\n`
      + `  ${column.COLUMN_NAME}: {\n`
      + `    primaryKey: true,\n`
      + `    type: Sequelize.${column.DATA_TYPE},\n`
      + `  },\n`
      : `  // ${column.COLUMN_COMMENT}\n`
      + `  ${column.COLUMN_NAME}: Sequelize.${column.DATA_TYPE},\n`
    }).join('')

    let outData = template.replace(/#{TableComment}#/g, tableData.TABLE_COMMENT)
            .replace(/#{TableName}#/g, tableData.TABLE_NAME)
            .replace(/#{ClassName}#/g, tableData.TABLE_NAME.split('_').map(item => item.slice(0, 1).toUpperCase() + item.slice(1)).join(''))
            .replace(/#{TableColumns}#/g, columnsStr)
            
    return outData
  }
}

module.exports = DataReplace
